package com.example.teststarter.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.function.Function;

/**
 * 员工类
 *
 * @author zsh
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee<T, C> {
    /**
     * 员工的名字
     */
    private String name;
    /**
     * 员工处理任务的逻辑
     */
    private Function<T, C> taskFunction;

    /**
     * 处理任务的方法
     *
     * @param boss 发布任务的老板对象，包含老板名称，任务及回调
     */
    protected void doTask(Boss<T, C> boss) {
        if (null == boss || null == boss.getTask()) {
            throw new IllegalArgumentException("boss is not ready...");
        }
        if (null == this.taskFunction) {
            throw new IllegalArgumentException("employee has no skills for task...");
        }
        //执行任务的实际方法
        C callback = taskFunction.apply(boss.getTask());
        //将结果发送给老板
        boss.callback(this, callback);
    }
}
